﻿using IDEAMS.core;
using IDEAMS.model;
using IDEAMS.model.etalon;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace IDEAMS.view.setting
{
    /// <summary>
    /// EtalonSettingDlg.xaml 的交互逻辑
    /// </summary>
    public partial class EtalonSettingDlg : Window
    {
        private EtalonSettingDlgViewModel windowViewModel;
        public EtalonSettingDlg()
        {
            InitializeComponent();

            windowViewModel = new EtalonSettingDlgViewModel();
            this.DataContext = windowViewModel;

            this.btnAdd.Click += new RoutedEventHandler(Add_Click);
            this.btnDelete.Click += new RoutedEventHandler(Delete_Click);
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            AddEtalonDlg dlg = new AddEtalonDlg();
            dlg.Owner = this;
            dlg.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            bool? result = dlg.ShowDialog();
            if(result == true)
            {
                Etalon etalon = new Etalon(dlg.windowViewModel.etalon);
                Etalon oldEtalon = TW_App.etalonCollection.Find(etalon.name);
                if (oldEtalon != null)
                {
                    TW_App.etalonCollection.Update(etalon.name, etalon);
                    TW_App.etalonCollection.Save();
                    windowViewModel.etalons.Clear();
                    TW_App.etalonCollection.ForEach(x =>
                    {
                        windowViewModel.etalons.Add(new EtalonViewModel(x));
                        return false;
                    });
                }
                else
                {
                    TW_App.etalonCollection.Add(etalon);
                    TW_App.etalonCollection.Save();
                    windowViewModel.etalons.Add(new EtalonViewModel(etalon));
                }
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show(Application.Current.MainWindow, "确认要删除标准具吗?", "删除确认", MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result == MessageBoxResult.No) return;

            List<EtalonViewModel> selectedItems = DGEtalonTable.SelectedItems.Cast<EtalonViewModel>().ToList();
            if (selectedItems == null || selectedItems.Count == 0) return;

            foreach (var v in selectedItems)
            {
                windowViewModel.etalons.Remove(v);
                TW_App.etalonCollection.Delete(v.name);
            }
            TW_App.etalonCollection.Save();
        }
    }

    public class EtalonSettingDlgViewModel : ViewModelBase
    {
        public EtalonSettingDlgViewModel()
        {
            etalons = new ObservableCollection<EtalonViewModel>();
            TW_App.etalonCollection.ForEach(x =>
            {
                etalons.Add(new EtalonViewModel(x));
                return false;
            });
        }

        private ObservableCollection<EtalonViewModel> m_etalons;
        public ObservableCollection<EtalonViewModel> etalons
        {
            get { return m_etalons; }
            set
            {
                m_etalons = value;
                NotifyPropertyChanged();
            }
        }
    }
}
